package guoanmaker.provider.business.dao;

import static org.hamcrest.CoreMatchers.nullValue;
import static org.mockito.Matchers.anyString;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import guoanmaker.provider.business.model.PersonalAddress;
import guoanmaker.provider.business.model.PersonalInvoice;
import guoanmaker.provider.business.model.ProvideInvoice;
import guoanmaker.provider.business.model.ProvideInvoiceInfo;
import guoanmaker.provider.business.model.ProvideOrderForm;
import guoanmaker.provider.business.modelnobind.OrderInvoiceAddress;
import guoanmaker.provider.business.modelnobind.ProvideOrderFormForOrder;
import guoanmaker.provider.business.repository.PersonalAddressRepository;
import guoanmaker.provider.business.repository.PersonalInvoiceRepository;
import guoanmaker.provider.business.repository.ProvideInvoiceInfoRepository;
import guoanmaker.provider.business.repository.ProvideOrderFormRepository;
import guoanmaker.provider.helper.jpa.Criteria;
import guoanmaker.provider.helper.jpa.Restrictions;

@Repository
public class ProvideInvoiceInfoDao {
	@PersistenceContext
	EntityManager em;
	
	@Resource
	private ProvideInvoiceInfoRepository provideInvoiceInfoRepository;
	
	@Resource
	private ProvideOrderFormRepository provideOrderFormRepository;
	
	@Resource
	private PersonalAddressRepository personalAddressRepository;
	
	@Resource
	private PersonalInvoiceRepository personalInvoiceRepository;
	/**
	 * 
	* @Description: 根据服务商id查询发票信息以及账户信息 
	* @param businessid 服务商id
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年7月28日 下午4:56:32     
	* @return Map    
	* @throws 
	 */
	public Map findInvoiceInfos(String businessid){
		Map map=new HashMap();
		String jpqlSumCommission="select sum(o.commission) from ProvideOrderForm o join o.commodity c join c.providerBusiness b where o.isCheck='1' and b.id='"+businessid+"'";
		String jpqlSunFee="select sum(o.fee) from ProvideOrderForm o join o.commodity c join c.providerBusiness b where o.isCheck='1' and  b.id='"+businessid+"'";
		String jpqlSumAmountOfMoney="select sum(i.amountOfMoney) from ProvideInvoiceInfo i where i.businessid='"+businessid+"'";
		
		Query querySumCommission = em.createQuery(jpqlSumCommission);
		List listSumCommission=querySumCommission.getResultList();
		BigDecimal sumCommission=(BigDecimal) listSumCommission.get(0);
		if(sumCommission==null){
			sumCommission=new BigDecimal(0.00);
		}
		
		Query querySunFee = em.createQuery(jpqlSunFee);
		List listSunFee=querySunFee.getResultList();
		BigDecimal sumFee=(BigDecimal) listSunFee.get(0);
		if(sumFee==null){
			sumFee=new BigDecimal(0.00);
		}
		
		Query querySumAmountOfMoney = em.createQuery(jpqlSumAmountOfMoney);
		List listSumAmountOfMoney=querySumAmountOfMoney.getResultList();
		BigDecimal sumAmountOfMoney=(BigDecimal) listSumAmountOfMoney.get(0);
		if(sumAmountOfMoney==null){
			sumAmountOfMoney=new BigDecimal(0.00);
		}
		
		BigDecimal sumAll=sumCommission.add(sumFee);
		
		Criteria<ProvideInvoiceInfo> criteria = new Criteria<>();
		criteria.add(Restrictions.like("businessid", businessid, true));
		List<ProvideInvoiceInfo> list = provideInvoiceInfoRepository.findAll(criteria);
		map.put("sumAll",sumAll );
		map.put("sumInvoicing", sumAmountOfMoney);
		map.put("sumNotInvoicing", sumAll.subtract(sumAmountOfMoney));
		map.put("result", list);
		return map;
	}
	
	/**
	 * 
	* @Description: 查询未开发票金额
	* @param businessid
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年7月28日 下午5:09:39     
	* @return BigDecimal    
	* @throws 
	 */
	public BigDecimal findsumNotInvoicing(String businessid){
		String jpqlSumCommission="select sum(o.commission) from ProvideOrderForm o join o.commodity c join c.providerBusiness b where o.isCheck='1' and b.id='"+businessid+"'";
		String jpqlSunFee="select sum(o.fee) from ProvideOrderForm o join o.commodity c join c.providerBusiness b where o.isCheck='1' and  b.id='"+businessid+"'";
		String jpqlSumAmountOfMoney="select sum(i.amountOfMoney) from ProvideInvoiceInfo i where i.businessid='"+businessid+"'";
		Query querySumCommission = em.createQuery(jpqlSumCommission);
		List listSumCommission=querySumCommission.getResultList();
		BigDecimal sumCommission=(BigDecimal) listSumCommission.get(0);
		if(sumCommission==null){
			sumCommission=new BigDecimal(0.00);
		}
		
		Query querySunFee = em.createQuery(jpqlSunFee);
		List listSunFee=querySunFee.getResultList();
		BigDecimal sumFee=(BigDecimal) listSunFee.get(0);
		if(sumFee==null){
			sumFee=new BigDecimal(0.00);
		}
		
		Query querySumAmountOfMoney = em.createQuery(jpqlSumAmountOfMoney);
		List listSumAmountOfMoney=querySumAmountOfMoney.getResultList();
		BigDecimal sumAmountOfMoney=(BigDecimal) listSumAmountOfMoney.get(0);
		if(sumAmountOfMoney==null){
			sumAmountOfMoney=new BigDecimal(0.00);
		}
		
		BigDecimal sumAll=sumCommission.add(sumFee);
		BigDecimal sumNotInvoicing=sumAll.subtract(sumAmountOfMoney);
		return sumNotInvoicing;
	}
	
	/**
	 * 
	* @Description: 根据服务商id查询发票详情 
	* @param businessid 服务商id
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年8月28日 下午8:08:12     
	* @return ProvideInvoice    
	* @throws 
	 */
	public ProvideInvoice findProvideInvoice(String businessid){
		ProvideInvoice invoice=new ProvideInvoice();
		String jpql = "select  i from ProvideInvoice i where i.businessid='"+businessid+"'";
		Query query = em.createQuery(jpql);
		List<ProvideInvoice> result = query.getResultList();
		if(result!=null&&result.size()>0){
			invoice=result.get(0);
		}
		return invoice;
		
	}
	
	/**
	 * 
	* @Description:查询订单发票详细信息
	* @param @param orderid
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年8月30日 上午11:35:30     
	* @return OrderInvoiceAddress    
	* @throws 
	 */
	public OrderInvoiceAddress getOrderInvoice(String orderid){
		OrderInvoiceAddress invoice=new OrderInvoiceAddress();
		ProvideOrderForm orderForm=provideOrderFormRepository.findOne(orderid);
		if(orderForm!=null){
			if(orderForm.getAddressid()!=null&&!orderForm.getAddressid().equals("")){
				PersonalAddress a=personalAddressRepository.findOne(orderForm.getAddressid());
				if(a!=null){
					invoice.setName(a.getName());
					invoice.setPostalAddress(a.getAddress());
					invoice.setPostalTelephone(a.getTelephone());
				}
			}
			if(orderForm.getInvoiceid()!=null&&!orderForm.getInvoiceid().equals("")){
				PersonalInvoice i=personalInvoiceRepository.findOne(orderForm.getInvoiceid());
				if(i!=null){
					invoice.setPrice(orderForm.getRealPrice());
					invoice.setInvoiceName(i.getInvoiceName());
					invoice.setTaxNumber(i.getTaxNumber());
					invoice.setInvoiceType(i.getInvoiceType());
					invoice.setRegisteredTelephone(i.getTelephone());
					invoice.setRegisteredaddress(i.getAddress());
					invoice.setBankName(i.getBankName());
					invoice.setBankNumber(i.getBankNumber());
					invoice.setBusinessLicensePic(i.getBusinessLicensePic());
					invoice.setBusinessPic(i.getBusinessPic());
				}
			}
			
		}
		
		return invoice;
	}
	
	/**
	 * 
	* @Description: 查询所有已申请的服务商发票 
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年11月17日 下午3:12:45     
	* @return List<ProvideInvoiceInfo>    
	* @throws 
	 */
	public List<ProvideInvoiceInfo> getProvideInvoiceInfos(){
		String jpql="select i from ProvideInvoiceInfo i where i.invoiceState='0' or i.invoiceState is null ";
		Query query = em.createQuery(jpql);
		List<ProvideInvoiceInfo> list = query.getResultList();
		return list;
	}
	
	/**
	 * 
	* @Description: 根据服务商id查询其发票基础信息id 
	* @param @param businessid
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年11月17日 下午3:56:57     
	* @return String    
	* @throws 
	 */
	public String getInvoiceId(String businessid){
		String jpql="select i from ProvideInvoice i where i.businessid='"+businessid+"'";
		Query query = em.createQuery(jpql);
		List<ProvideInvoice> list= query.getResultList();
		String invoiceid="";
		if(list!=null&&list.size()>0){
			invoiceid=list.get(0).getId();
		}
		return invoiceid;
	}
	
	/**
	 * 
	* @Description:查询财务平台返回的id是否已有数据
	* @param @param id
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年11月17日 下午4:35:29     
	* @return String    
	* @throws 
	 */
	public String getInvoiceCaiwuid(String id){
		String jpql="select i from ProvideInvoiceInfo i where i.caiwuid='"+id+"'";
		String result="1";
		Query query = em.createQuery(jpql);
		List<ProvideInvoiceInfo> list = query.getResultList();
		if(list!=null&&list.size()>0){
			result="2";
		}
		return result;
	}

}
